<?php
namespace app\admin\controller;
use \think\facade\View;
use \think\facade\Request;
use \think\facade\Db;
use \think\facade\Cache;
class Goods
{

    public function goodList()
    {

        $post=Request::param();
        View::assign($post);
        $where = [];
        if(isset($post['is_down']) && count($post['is_down'])==1) $where['is_down']=$post['is_down'];
        if(isset($post['is_show']) && $post['is_show'] <> '')     $where['is_show']=$post['is_show'];
        if(isset($post['is_hot']) && $post['is_hot'] <> '')       $where['is_hot']=$post['is_hot'];

        if(isset($post['k_time']) && !empty($post['k_time']))     $where['k_time']=$post['k_time'];
        if(isset($post['j_time']) && !empty($post['j_time']))     $where['j_time']=$post['j_time'];
        if(isset($post['d_price']) && !empty($post['d_price']))     $where['d_price']=$post['d_price'];
        if(isset($post['g_price']) && !empty($post['g_price']))     $where['g_price']=$post['g_price'];


        if(isset($post['keywords']) && $post['keywords']<>'')    $where['keywords'] = $post['keywords'];
        if(isset($post['cid'])     && !empty($post['cid']))         $where['g.cid']=$post['cid'];
        $goodlist=\app\admin\model\Goods::getGoodlist($where);
        View::assign('goodlist',$goodlist);
        $cate=\app\admin\model\Category::getCategory();
        $cate = $cate->toArray();
        foreach ($cate as &$v){
            $v['next']=Db::name('category')->where('parentid',$v['id'])->select()->toArray();
        }
        View::assign('cate',$cate);

        return view('good_list');
    }

    public function goodAdd()
    {
        return view('good_add');
    }

    public function cate()
    {

        $cate=\app\admin\model\Category::getCate();


        $cate= $cate ->toArray();
//        foreach ( $cate as &$v){
//            //$cate[$k]['next'] = '';
//            $v['next']=Db::name('category')
//                ->where('parentid',$v['id'])
//                ->count();
//        }
        //缓存  has set get
        //参数  set (变量名 , 变量 ,时间)
        //判断缓存是否设置

        if(Cache::has('cate')){
            $all=Cache::get('cate');
            //获取缓存信息
        }else{
            $all=Db::name('category')
                ->distinct(true)
                ->column('parentid');
            //存储进缓存文件中
            Cache::set('cate',$all,10);
        }
        foreach ($cate as &$v){
            //判断当前分类id 在所有的分类父ID种出现 则有下一级
            if (in_array($v['id'],$all)){
                $v['next']=1;
            }else{
                $v['next']=0;
            }
        }



        View::assign('cate',$cate);
        View::assign('empty','<tr><td colspan="5" style="text-align: center">没有数据了</td></tr>');
        return view('cate');
    }


}